// const { Children } = require("react");

const App = {
  mixins: [Save],//配置文件，路径/utils/mixins.js
  data() {
    return {
      reverse: true,
      activities: [],
      category: [],
      Manager: [],
      dyManager: [],
      options: [],
      checkAll: false,
      indeterminate: false,
      dataForm: {
        // Row_Status :'1'
      },


      value: '',




      ID_Card: [],

      dataForm1: {

      },
      rules: {
        // 身份证编号验证（空值+格式）
        ID_Card_Number: [
          {
            required: true,
            message: "身份证编号不能为空",
            trigger: "blur",
          },
          {
            pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
            message: "身份证格式不正确",
            trigger: "blur",
          },
        ],

      },
      personal_information: [],
      Manager: [],
      Nation: [],
      Ducation: ducation(),
      city: [],
      cityValue: [],
      Category: [],
      is: false,




    }
  },
  mounted() {
    this.getData();
    if (this.nodeKey) {
      this.shibie(this.nodeKey)
    }
  },
  computed: {
    activities1() {
      let arr = [];
      this.activities.filter(i=>i.Row_Status !='1').forEach(item => {
        if (!arr.some(i => i.Personal_Tag_SerialNumber == item.Personal_Tag_SerialNumber)) {
          arr.push(item);
        }
      })
      console.log(arr);
      return arr
    }
  },
  watch: {
    'dataForm.ID_Card_Number'(val) {
      this.dataForm1.ID_Card_Number = val;
    },
    'dataForm.Party_Member_SerialNumber'() {
      if (this.action == 'Modify') {
        $.ajax({
          headers: requestHeaders(),
          url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
          method: "post",
          data: {
            Data: "personal_information", //表名
            Condition: "ID_Card_Number='" + this.dataForm.ID_Card_Number + "'",
          },
          success: (res) => {
            if (res.length == 1) {
              this.dataForm1 = res[0];
              this.cityValue = [this.dataForm1.RR_Province_Number, this.dataForm1.RR_City_Number, this.dataForm1.RR_County_Nubmer, this.dataForm1.RR_Town_Number];
              this.is = false;
            } else {
              this.is = true;
            }
          },
          complete: (XMLHttpRequest) => {
            requestPretreatment(XMLHttpRequest)
          },
        });
      }
    }
  },
  methods: {
    shibie(val) {
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
        method: "post",
        data: {
          Data: "personal_information", //表名
          Condition: "ID_Card_Number='" + val + "'",
        },
        success: (res) => {
          if (res.length == 1) {
            //  let obj = {a:'10',b:'20'}
            console.log(res)
            this.dataForm = { ...this.dataForm, ...res[0] };

            this.is = false;
          } else {
            this.is = true;
          }
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest)
        },
      });
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
        method: "post",
        data: {
          SQLSerialNumber: "f_personal_mapping_tags", //表名
          Condition: '',
          ExtensionCondition: utf8ToBase64("AND ID_Card_Number='" + val + "'"),
        },
        success: (res) => {
          this.activities = res
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest)
        },
      });
      // $.ajax({
      //   headers: requestHeaders(),
      //   url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
      //   method: "post",
      //   data: {
      //     Data: 'personal_mapping_tags',
      //     Condition: `ID_Card_Number='${val}' AND Row_Status='0'`,
      //   },
      //   success: (res) => {
      //     this.childDataForm = res;
      //   },
      //   complete: (XMLHttpRequest) => {
      //     requestPretreatment(XMLHttpRequest)
      //   },
      // });
    },
    getData() {
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableName`,
        type: "POST",
        data: {
          Data: 'personal_tags',//标注人员类型表名称
          Condition: "Row_Status='0'",
        },
        success: (data) => {
          this.options = data; // 直接使用 this
        },
        complete: (XMLHttpRequest) => {
          requestPretreatment(XMLHttpRequest)
        },
        dataType: "json",
      });
      $.ajax({
        headers: requestHeaders(),
        url: `${mh}/generalBusiness/generalQueryByTableName`,
        type: "POST",
        data: {
          Data: 'personal_information',
          Condition: "Row_Status='0'",
        },
        success: (data) => {
          console.log(data);
          this.personal_information = data; // 直接使用 this
        },
        complete: (XMLHttpRequest) => {

          requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    handleClose(item) {
      this.$prompt('此操作将取消该标签, 请填写原因?', '提示', {
        inputType: 'textarea', // 关键代码
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        inputValidator: (value) => {
          // 如果值为空，返回错误提示
          if (!value || value.trim() === '') {
            return '内容不能为空！';
          }
          return true; // 验证通过
        },
      }).then(({ value }) => {
        $.ajax({
          headers: requestHeaders(),
          url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
          method: "post",
          data: {
            Data: 'personal_mapping_tags',
            Condition: `Personal_Mapping_Tag_SerialNumber='${item.Personal_Mapping_Tag_SerialNumber}'`,
          },
          success: (res) => {
            res[0].Row_Status = '1';
            res[0].Cancel_Reason = value;
            let lJSONObject_DataSet = {
              Modify: {
                personal_mapping_tags:res
              }
            }
            submitSave(lJSONObject_DataSet, (res) => {
              if (res.Status == 'success') {
                this.shibie(this.nodeKey)
                this.$message({
                  type: 'success',
                  message: '取消成功!'
                });
              }
            })
          },
          complete: (XMLHttpRequest) => {
            requestPretreatment(XMLHttpRequest)
          },
        });
      })
    },
    changeCity(val) {
      if (val.length > 0) {
        let Province = this.city.filter(i => i.value == val[0])[0];
        let City = Province.children.filter(i => i.value == val[1])[0];
        let County = City.children.filter(i => i.value == val[2])[0];
        let Town = County.children.filter(i => i.value == val[3])[0];
        this.dataForm1.Registered_Residence = Province.label + City.label + County.label + Town.label;
        this.dataForm1.RR_Province_Number = val[0];
        this.dataForm1.RR_City_Number = val[1];
        this.dataForm1.RR_County_Nubmer = val[2];
        this.dataForm1.RR_Town_Number = val[3];
      } else {
        this.dataForm1.Registered_Residence = '';
        this.dataForm1.RR_Province_Number = '';
        this.dataForm1.RR_City_Number = '';
        this.dataForm1.RR_County_Nubmer = '';
        this.dataForm1.RR_Town_Number = '';
      }
    },
    async handleSubmit() {
      const allValid = await this.validateAllForms();
      if (allValid) {
        this.loading = true;
        let ls_URL = window.location.href;
        const action = getQueryObject(ls_URL, "action");   // 获取 action 参数
        let lJSONObject_Data_Options = new Object();
        //获取主表配置参数
        lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));

        let ls_MainTableName = lJSONObject_Data_Options.TableName;
        let ls_DataForm = lJSONObject_Data_Options.form;
        let ls_primary_key = lJSONObject_Data_Options.primary_key;
        //没有子表下面内容就会报错，try/catch抛出异常
        try {
          //可能涉及多个子表
          document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
            let lJSONObject_ChildData_Options = new Object();
            lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

            let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
            let ls_ChildDataForm = lJSONObject_ChildData_Options.form;
            // let ls_ParentTableName = lJSONObject_ChildData_Options.ParentTableName;
            if (this[ls_DataForm][ls_primary_key]) {
              this[ls_ChildDataForm].forEach(item => {
                item[ls_primary_key] = this[ls_DataForm][ls_primary_key];
              })
            }
            //建立子表结构体
            let JSONArray_ChildRecord = new Array();
            JSONArray_ChildRecord = this[ls_ChildDataForm];
            if (!this[ls_DataForm].Children) {
              this[ls_DataForm].Children = new Object();
            }
            this[ls_DataForm].Children[ls_ChildTableName] = JSONArray_ChildRecord;
          })
        } catch (error) {

        }
        //建立新增提交结构
        let lJSONObject_DataSet = new Object();
        if (action == 'Add') {

          //改动后
          lJSONObject_DataSet = {
            Add: {
              personal_mapping_tags: this.childDataForm
            }
          }
          console.log(lJSONObject_DataSet);

        } else if (action == 'Modify') {
          const { add, modify } = this.deal(this[ls_DataForm]);
          let lJSONArray_Record = new Array();
          lJSONArray_Record.push(modify);

          let lJSONObject_Table = new Object();
          lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;

          lJSONObject_DataSet[action] = lJSONObject_Table;
          lJSONObject_DataSet['Add'] = new Object();
          if (add != null) {
            let lJSONArray_Record_Add = new Array();
            lJSONArray_Record_Add.push(add);

            let lJSONObject_Table_Add = new Object();
            lJSONObject_Table_Add[ls_MainTableName] = lJSONArray_Record_Add;

            lJSONObject_DataSet['Add'] = lJSONObject_Table_Add;
          }
        }
        let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
        lJSONObject_DataSet['Add']['base_operation_log'] = [{
          Operation_Type: '0',
          Operation_Path: this.relativePath,
          Operation_Main,
          Operation_Interface: "generalSaveByRequestBody",
          Operation_IP: getIpAddress()
        }]
        submitSave(lJSONObject_DataSet, (res) => {
          if (res.Status == 'success') {
            this.shibie(this.dataForm.ID_Card_Number)
            window.history.go(-1);
          }
          this.loading = false;

        })
      }
    },
  },

};


const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component);
}
app.component('yl-select', ylSelect);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
